package com.jlastu.znb.provider;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.AbstractWindowedCursor;
import android.database.Cursor;
import android.database.CursorWindow;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.jlastu.znb.database.DBHelper;

public class ZNBProvider extends ContentProvider {

   private static final String AUTHORITY = "com.jlastu.znb.provider";
   private static UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
   private static final int MATCHER_GOODS = 1;
   private static final int MATCHER_GOODS_ID = 2;
   private static final int MATCHER_NEWS = 3;
   private static final int MATCHER_NEWS_ID = 4;
   private static final int MATCHER_USER = 11;
   private static final int MATCHER_USER_ID = 12;
   private static final int MATCHER_PHOTO = 21;
   private static final int MATCHER_NEWSPHOTO = 22;
   private static final int MATCHER_GOODS_BY_CATEGORY = 5;
   private static final int MATCHER_NEWS_BY_CATEGORY = 6;

   private DBHelper mDBHelper;

   static {//通配符：*所有字符，#所有数字
      // content://com.jlastu.znb.provider/goods
      uriMatcher.addURI(AUTHORITY, "goods", MATCHER_GOODS);
      // content://com.jlastu.znb.provider/goods/1
      uriMatcher.addURI(AUTHORITY, "goods/#", MATCHER_GOODS_ID);
      uriMatcher.addURI(AUTHORITY, "news", MATCHER_NEWS);
      uriMatcher.addURI(AUTHORITY, "news/#", MATCHER_NEWS_ID);
      uriMatcher.addURI(AUTHORITY, "user", MATCHER_USER);
      uriMatcher.addURI(AUTHORITY, "user/#", MATCHER_USER_ID);
      uriMatcher.addURI(AUTHORITY, "photo", MATCHER_PHOTO);
      uriMatcher.addURI(AUTHORITY, "newsphoto", MATCHER_NEWSPHOTO);
      uriMatcher.addURI(AUTHORITY, "goods_by_category/*", MATCHER_GOODS_BY_CATEGORY); // 匹配按分类查询的 URI
      uriMatcher.addURI(AUTHORITY, "news_by_category/*", MATCHER_NEWS_BY_CATEGORY); // 匹配按分类查询的 URI
   }

   @Override
   public boolean onCreate() {
      mDBHelper = new DBHelper(getContext());
      // 注意：返回 true 表示成功创建提供者
      return true;
   }

   @Nullable
   @Override
   public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
      SQLiteDatabase db = mDBHelper.getReadableDatabase();
      Cursor cursor = null;
      switch (uriMatcher.match(uri)) {
         case MATCHER_GOODS:
            return db.query("goods", projection, selection,
                    selectionArgs, null, null, sortOrder);
         case MATCHER_GOODS_ID:
            long goods_id = ContentUris.parseId(uri);
            return db.query("goods", projection, "_id = ?",
                    new String[]{String.valueOf(goods_id)}, null,
                    null, sortOrder, null);
         case MATCHER_NEWS:
            return db.query("news", projection, selection,
                    selectionArgs, null, null, sortOrder);
         case MATCHER_NEWS_ID:
            long news_id = ContentUris.parseId(uri);
            return db.query("news", projection, "_id = ?",
                    new String[]{String.valueOf(news_id)}, null,
                    null, sortOrder, null);
         case MATCHER_USER:
            return db.query("user", projection, selection,
                    selectionArgs, null, null, sortOrder);
         case MATCHER_USER_ID:
            long user_id = ContentUris.parseId(uri);
            Log.d("provider", "user_id ========= " + user_id);
            return db.query("user", projection, "_id = ?",
                    new String[]{String.valueOf(user_id)}, null,
                    null, sortOrder, null);
         case MATCHER_PHOTO:
            cursor = db.query("photo", projection, selection,
                    selectionArgs, null, null, sortOrder);
            /*CursorWindow cw = new CursorWindow("test", 5000000);
            AbstractWindowedCursor ac = (AbstractWindowedCursor) cursor;
            ac.setWindow(cw);*/
            return cursor;
         case MATCHER_NEWSPHOTO:
            cursor = db.query("newsphoto", projection, selection,
                    selectionArgs, null, null, sortOrder);
            /*CursorWindow cw = new CursorWindow("test", 5000000);
            AbstractWindowedCursor ac = (AbstractWindowedCursor) cursor;
            ac.setWindow(cw);*/
            return cursor;
         case MATCHER_GOODS_BY_CATEGORY:
            // 从 URI 中获取最后一个路径段，这应该是您的分类参数
            String category = uri.getPathSegments().get(1); // 确保这个值是正确的
            // 使用该分类参数进行数据库查询
            cursor = db.query("goods", projection, "category = ?", new String[]{category}, null, null, sortOrder);
            break;
         case MATCHER_NEWS_BY_CATEGORY:
            // 从 URI 中获取最后一个路径段，这应该是您的分类参数
            category = uri.getPathSegments().get(1);
            // 使用该分类参数进行数据库查询
            cursor = db.query("news", projection, "category = ?", new String[]{category}, null, null, sortOrder);
            break;
        
      }
      return cursor;
   }
   @Nullable
   @Override
   public String getType(@NonNull Uri uri) {
      switch (uriMatcher.match(uri)) {
         case MATCHER_GOODS:
            return "vnd.android.cursor.dir/goods";
         case MATCHER_GOODS_ID:
            return "vnd.android.cursor.item/goods";
         case MATCHER_NEWS:
            return "vnd.android.cursor.dir/news";
         case MATCHER_NEWS_ID:
            return "vnd.android.cursor.item/news";
         case MATCHER_USER:
            return "vnd.android.cursor.dir/user";
         case MATCHER_USER_ID:
            return "vnd.android.cursor.item/user";
         case MATCHER_PHOTO:
            return "vnd.android.cursor.dir/photo";
         case MATCHER_NEWSPHOTO:
            return "vnd.android.cursor.dir/newsphoto";
         case MATCHER_GOODS_BY_CATEGORY:
            return "vnd.android.cursor.dir/goods_by_category";
         case MATCHER_NEWS_BY_CATEGORY:
            return "vnd.android.cursor.dir/news_by_category";
         default:
            throw new IllegalArgumentException("Unsupported URI: " + uri);
      }
   }


   @Nullable
   @Override
   public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
      SQLiteDatabase db = mDBHelper.getWritableDatabase();
      long insertId = -1;
      switch (uriMatcher.match(uri)) {
         case MATCHER_GOODS:
            insertId = db.insert("goods", null, values);
            break;
         case MATCHER_NEWS:
            insertId = db.insert("news", null, values);
            break;
         case MATCHER_USER:
            insertId = db.insert("user", null, values);
            break;
         case MATCHER_PHOTO:
            insertId = db.insert("photo", null, values);
            break;
         case MATCHER_NEWSPHOTO:
            insertId = db.insert("newsphoto", null, values);
            break;
      }
      return ContentUris.withAppendedId(uri, insertId);
   }

   @Override
   public int bulkInsert(@NonNull Uri uri, @NonNull ContentValues[] values) {
      for (ContentValues contentValues : values) {
         Log.d("", "bulkInsert ===== ");
         insert(uri, contentValues);
      }
      return super.bulkInsert(uri, values);
   }

   @Override
   public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
      SQLiteDatabase db = mDBHelper.getWritableDatabase();
      switch (uriMatcher.match(uri)) {
         case MATCHER_GOODS:

         case MATCHER_GOODS_ID:
            long goods_id = ContentUris.parseId(uri);

         case MATCHER_USER:

         case MATCHER_USER_ID:
            long user_id = ContentUris.parseId(uri);

      }
      return 0;
   }

   @Override
   public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
      SQLiteDatabase db = mDBHelper.getWritableDatabase();
      int rowsAffected = 0;
      switch (uriMatcher.match(uri)) {
         case MATCHER_GOODS:
            // 更新整个goods表
            rowsAffected = db.update("goods", values, selection, selectionArgs);
            break;
         case MATCHER_GOODS_ID:
            long goods_id = ContentUris.parseId(uri); // 解析ID
            // 确保selectionArgs包含goods_id
            String[] selectionArgsWithId = new String[]{String.valueOf(goods_id)};
            rowsAffected = db.update("goods", values, "_id = ?", selectionArgsWithId);
            break;
         case MATCHER_USER:
            // 更新user表
            rowsAffected = db.update("user", values, selection, selectionArgs);
            break;
         case MATCHER_USER_ID:
            long user_id = ContentUris.parseId(uri); // 解析ID
            // 确保selectionArgs包含user_id
            String[] selectionArgsWithUserId = new String[]{String.valueOf(user_id)};
            rowsAffected = db.update("user", values, "_id = ?", selectionArgsWithUserId);
            break;
      }
      return rowsAffected;
   }
}
